(function() {
	let els = {};
	els.next = $('#next');
	els.imgs = $('img');
	els.step1 = $('#step1');
	els.step2 = $('#step2');
	els.step3 = $('#step3');
	els.confirm = $('#confirm');
	let step = 1;
	let count = 0;
	let scroll = 0;
	let nextTime = 0;
	
	init();
	function init() {
		listenNext();
		els.next.click();
		getUserInfo(setNickname);
	}
	
	
	// let siv = setInterval(() => {
	// 	showImg(els.imgs[i]);
	// 	i++;
	// 	if (i > 2) clearInterval(siv);
	// }, 2000);
	
	// 监听继续按钮
	function listenNext() {
		els.next.onclick = () => {
			let nowTime = new Date().getTime();
			if (nowTime - nextTime < 1000) return;
			nextTime = nowTime;
			if (step == 1 && count == 3) {
				step = 2;
				closeStep(els.step1);
				setTimeout(() => {
					showImg(els.imgs[count]);
					count++;
				}, 1000);
			}
			else if (step == 2 && count == 7) {
				console.log('step 3');
				step = 3;
				closeStep(els.step2);
				els.next.style.display = 'none';
				setTimeout(() => {
					showProxy(els.step3);
				}, 1000);
			}
			else {
				showImg(els.imgs[count]);
				count++;
			}
		}
	}
	
	// 页面滚动到底部
	function pageToBottom() {
		let h = screen.height;
		let y = document.body.offsetHeight;
		let sy = document.documentElement.scrollTop;
		let dh = y - h;
		// console.log(dh);
		if (dh < 0) return false;
		let ty = scroll;
		let siv = setInterval(() => {
			ty += 2;
			window.scrollTo(0, ty);
			if (ty >= dh) {
				clearInterval(siv);
				scroll += dh;
			}
		}, 16.66);
	}
	
	// 显示图片
	function showImg(img) {
		// console.dir(img);
		img.style.display = 'block';
		img.className =('show');
		pageToBottom();
	}
	// 关闭步骤
	function closeStep(step) {
		// console.log(step);
		step.classList.add('hide');
		setTimeout(() => {
			step.style.display = 'none';
		}, 1000);
	}
	// 显示我托书
	function showProxy(step) {
		step.style.display = 'flex';
		els.confirm.onclick = () => {
			let userid = getCookie('userid');
			let usgid = getItem('usersGuideId');
			usgid = usgid ? usgid : {};
			if (usgid) {
				if (usgid[userid]) usgid[userid].gid = 1;
				else usgid[userid] = {gid: 1};
			}
			else usgid[userid] = {"gid": 1};
			setItem('usersGuideId', usgid);
			updateGuideId();
			setTimeout(() => {
				location.replace("home.html");
			}, 1000);
		}
	}
	// 获取用户信息
	function setNickname() {
		let info = getItem('info');
		if (!info) return;
		let dt = new Date();
		let y = dt.getFullYear();
		let m = dt.getMonth()+1;
		let d = dt.getDate();
		let datetime = y+'年'+m+'月'+d+'日';
		document.getElementById('nickname').innerText = '亲爱的'+info.nick_name;
		document.getElementById('datetime').innerText = '签发日期：'+datetime;
	}
	
	// 上传引导步骤
	function updateGuideId() {
		let token = getCookie('token');
		ajax({
			url: DOMAIN + '/home/guide/set_record',
			header: {
				'Authorization': token,
			},
			data: {
				bu: 1
			},
			success: res => {
				if (res.code != 1) openTips(res.msg);
			}
		});
	}
})();